/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','materialment');

/*controller部分*/


//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
})


//主要操作controller
authApp.controller("expertsListController",function($scope,$http,$getQueryString,$location,$splice){
	//针对此页面需要的参数 should edit
	$scope.isAddM=true;
	$scope.expertPid=$getQueryString("pid");
	
	//**********主体方法
	var queryMainApi=apis.queryExpertInGroup;
	$scope.queryMain=function(){
		$http({
			method:'post',
			url:queryMainApi,
			data:{'groupId':$scope.expertPid},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		$scope.list=response.data;

		angular.forEach($scope.list,function(data,index,array){
			if(data.s_identity=="1"){
				data.identitysName="组员";
			}else if(data.s_identity=="2"){
				data.identitysName="组长";
			}else if(data.s_identity=="3"){
				data.identitysName="联络员";
			}
		})
	}	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	//按钮添加事件
	$scope.addM=function(){
		$scope.isAddM=false;
		$scope.getExpertList();	
	}
	
	//按钮取消事件
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.addFZ="";
	}

	//将数据用","拼接起来
	var selStr = ""; // 原来存放选中的项
	$scope.check = function(id,x){			
		if(x == true){     // 选中
			selStr=selStr+id+':';
		}else{
		    selStr=selStr.replace(id+':',''); // 取消选中
		}
	    $scope.conStr = selStr; // 页面所选请求ID
	};
	
	//现场考察专家组-添加组员
	$scope.addAuthExperts=function(){
		if(window.confirm("是否确定添加？")){
			$scope.conStr = $scope.conStr.substring(0,$scope.conStr.length-1);
			var rJson={'groupId':$scope.expertPid,'expertlList':$scope.conStr, 'group_type': 1};
			console.log(rJson);
			$http({
				method:'post',
				url:apis.addExpertToGroup,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
					selStr="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
			})
		}
	}
	
	//查询未绑定现场考察专家组的所有专家
	$scope.getExpertList=function(){		
		$http({
			method:'post',
			url:apis.queryExpertNotInGroup,
			data:{'group_type': 1}
		}).success(function(response){
			if(response.code=="000000"){
				$scope.expertList=response.data;
				$scope.Selected = false;//默认未选中
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	
	//删除现场考察专家组组员
	$scope.delCollect=function(pid){
		if(window.confirm("是否确定删除？")){
			var rJson={'groupId':$scope.expertPid,'expertlList':pid};
			$http({
				method:'post',
				url:apis.deleteExpertInGroup,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
	
	//设置现场考察专家组 组员身份
	$scope.setAuthExport=function(pid,ident){
		if(window.confirm("是否确定修改专家身份？")){
			var rJson={'groupId':$scope.expertPid,'expertId':pid,'identity':ident};
			$http({
				method:'post',
				url:apis.setPowerOfExpert,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.queryMain();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
	
	$scope.turnTo=function(){
		location.href="materialment.html";
	}
	
})
